package sparrowGui.components
{
	import flash.events.MouseEvent;
	
	import sparrowGui.components.item.ItemFactory;
	import sparrowGui.components.item.SListItem;
	import sparrowGui.components.item.SRichItem;
	import sparrowGui.data.ListSelectionData;
	import sparrowGui.event.ListEvent;
	import sparrowGui.i.IItem;
	import sparrowGui.i.IItemFactory;
	import sparrowGui.i.IListSelectionData;
	
	[Event(name="list_item_select", 	type="sparrowGui.event.ListEvent")]
	[Event(name="list_item_create", 	type="sparrowGui.event.ListEvent")]
	[Event(name="list_item_update", 	type="sparrowGui.event.ListEvent")]
	
	/**
	 * 单选列表控件
	 * 跟itemList有很多相似的功能,不同的是此组件的显示项是固定绑定的,并不是动态产生.
	 * 
	 * 例子如下
	 * 
		var ls:ItemGroup = new SRadioGroup();
		ls.update([11,22,33,44]);
		addChild(ls);
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SRadioGroup extends ItemGroup
	{
		private var _data:Object;
		protected var selectData:IListSelectionData;
		
		/**
		 * 构造单选组件
		 * @param uiVars 皮肤变量
		 * @param cellFactory 子项工厂，用于创建子项和子项皮肤，工厂里还有缓存的功能
		 */
		public function SRadioGroup(uiVars:Object=null,cellFactory:IItemFactory=null)
		{
			cellFactory = cellFactory || new ItemFactory(SListItem,"radioItem");
			selectData = new ListSelectionData();
			selectData.addListSelectionListener(onSelectChange);
			selectData.multiSelect = false;
			super(uiVars,cellFactory);
			colNum = 0;
			addSkinListen();
		}
		
		/**
		 * 通过数据来更新和创建组件,此方法会先把容器清空再跟椐数据建立新项
		 */
		override public function update(data:Object):void
		{
			selectData.removeAllSelect();
			removeAllItems();
			createItems(data.length);
			updateItems(data);
			arrangeItems();
			addSkinListen();
			if(selectData.mustSelect && itemLs.length>0)
				selectData.setSelect(0);
		}
		
		override public function updateItems(data:Object):void
		{
			super.updateItems(data);
			if(selectData.mustSelect && itemLs.length>0)
				selectData.setSelect(0);
		}
		
		override public function addItem(itm:IItem):IItem
		{
			super.addItem(itm);
			dispatchEvent(new ListEvent(ListEvent.LIST_ITEM_CREATE,null,itm));
			return itm;
		}
		
		override protected function updateItem(itm:IItem, data:Object=null):void
		{
			super.updateItem(itm,data);
			dispatchEvent(new ListEvent(ListEvent.LIST_ITEM_UPDATE,data,itm));
		}
		
		override protected function addSkinListen():void
		{
//			skin.addEventListener(MouseEvent.CLICK,onClick);
			for each (var itm:IItem in itemLs) 
			{
				if(!itm) continue;
				itm.skin.addEventListener(MouseEvent.CLICK,onClick);
			}
		}
		
		override protected function removeSkinListen():void
		{
//			skin.removeEventListener(MouseEvent.CLICK,onClick);
			for each (var itm:IItem in itemLs) 
			{
				if(!itm) continue;
				itm.skin.removeEventListener(MouseEvent.CLICK,onClick);
			}
		}
		
		private function onClick(e:MouseEvent):void
		{
			var clickName:String = e.currentTarget.name;
			var itm:IItem = getItemByName(clickName);
			var cid:int = itemLs.indexOf(itm);
			selectData.setSelect(cid);
		}
		
		private function onSelectChange(e:ListEvent):void
		{
			var m:IListSelectionData = selectData;
			for (var i:int = 0; i < itemLs.length; i++) 
			{
				var itm:IItem = itemLs[i] as IItem;
				var itmSelected:Boolean = m.isSelect(i);
				itm.setState(SListItem.SELECTSTATE_NAME,itmSelected);
			}
			if(m.getSelectIndex()>=0)
			{
				itm = getItemAt(m.getSelectIndex());
				var itData:Object = (itm==null)?null:itm.itemData;
			}
			dispatchEvent(new ListEvent(ListEvent.LIST_ITEM_SELECT,itData,itm));
		}
		
		override public function removeAllItems():void
		{
			removeSkinListen();
			super.removeAllItems();
		}
		
		override public function dispose():void
		{
			removeSkinListen();
			selectData.removeListSelectionListener(onSelectChange);
			selectData = null;
			super.dispose();
		}
		
		/////////////////////////////////////////
		// get/set
		/////////////////////////////////////////
		
		/**
		 * 通过项index获取项(data是数组的时候可用,如果data是哈希数据,可以用getItemByArgs)
		 
		public function getItemAt(itemName:String):RichItem
		{
			return itemTarget[itemName] as RichItem;
		}*/
		
		override public function set enabled(value:Boolean):void
		{
			super.enabled = value;
			for each (var itm:SRichItem in itemHash) 
			{
				itm.enabled = value;
			}
		}
		
		/**
		 * 是否激活可用
		 */
		override public function get enabled():Boolean
		{
			return super.enabled;
		}
		
		override public function set visible(value:Boolean):void
		{
			super.visible = value;
			for each (var itm:SRichItem in itemHash) 
			{
				itm.visible = value;
			}
		}
		
		/**
		 * 是否显示
		 */
		override public function get visible():Boolean
		{
			return super.visible;
		}
		
		/**
		 * 数据
		 */
		public function get data():Object
		{
			return _data;
		}
		
		/**
		 * 是否多选(设为true的时候跟CheckBox功能一样，只是皮肤不同而已)
		 * @param value
		 */
		public function get multiSelect():Boolean
		{
			return selectData.multiSelect;
		}

		/**
		 * 是否多选(设为true的时候跟CheckBox功能一样，只是皮肤不同而已)
		 * @param value
		 */
		public function set multiSelect(value:Boolean):void
		{
			selectData.multiSelect = value;
		}
		
		/**
		 * 是否至少有一个项被选中
		 */
		public function get mustSelect():Boolean
		{
			return selectData.mustSelect;
		}
		
		/**
		 * @private
		 */
		public function set mustSelect(value:Boolean):void
		{
			selectData.mustSelect = value;
		}
		
		/**
		 * 设置选中项
		 * @param index
		 */
		public function setSelect(index:int):void
		{
			selectData.setSelect(index);
		}
		
		/**
		 * 获取选中的项索引
		 * @return 
		 */
		public function getSelectIndex():int
		{
			return selectData.getSelectIndex();
		}
		
		/**
		 * 获取选中的项
		 * @return 
		 */
		public function getSelectItem():IItem
		{
			return itemLs[getSelectIndex()] as IItem;
		}
		
		override public function getDefaultUIName():String
		{
			return "radioGroup";
		}
	}
}